package io;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class IODemo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        copyDemo();
        oosOisDemo();
        readerWriter();
    }

    private static void readerWriter() throws IOException {
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("asd.txt"), StandardCharsets.UTF_8);
        osw.write("qwer一二三");
        osw.close();
        InputStreamReader isr = new InputStreamReader(new FileInputStream("asd.txt"),StandardCharsets.UTF_8);
        char [] data = new char[1024];
        int len ;
        while((len = isr.read(data)) != -1){
            System.out.println(Arrays.copyOf(data,len));
        }
        isr.close();
     }

    private static void oosOisDemo() throws IOException, ClassNotFoundException {
        String name ="李四";
        int age = 25;
        String gender = "女";
        String [] otherData = {"4","5",};
        Student ls = new Student(name ,age ,gender,otherData);
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student_对象输出流.obj"));
        oos.writeObject(ls);
        oos.close();
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student_对象输出流.obj"));
        Student stu = (Student)ois.readObject();
        ois.close();
        System.out.println(stu);
    }

    private static void copyDemo() throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("youdao.jpg"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("youdao_缓冲流复制.jpg"));
        int d;
        while ((d = bis.read()) != -1){
            bos.write(d);
        }
        bis.close();
        bos.close();
    }
}
